package advs;

import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.Document;
import net.jxta.document.Element;
import net.jxta.document.ExtendableAdvertisement;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.id.ID;

public class ServiceAdvertisement extends ExtendableAdvertisement
{
	String ServiceName;
	String pipeAdvID;
	String solverAdvID;
	String solverID;
	String id;

	public static String ServiceNameTag = "ServiceName";

	public static String PipeAdvIDTag = "PipeAdvID";

	public static String SolverAdvIDTag = "SolverAdvID";
	
	public static String SolverIDTag = "SolverID";

	public static String IDTag = "ID";

	String[] fields = new String[]
	{ ServiceNameTag };

	public ServiceAdvertisement()
	{
		super();
	}

	@Override
	public String getBaseAdvType()
	{
		return ServiceAdvertisement.getAdvertisementType();
	}

	@Override
	public ID getID()
	{
		return ID.nullID;
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	@Override
	public String[] getIndexFields()
	{
		return fields;
	}

	public static String getAdvertisementType()
	{
		return "solver:sa";
	}

	public static class Instantiator implements
			AdvertisementFactory.Instantiator
	{

		@Override
		public String getAdvertisementType()
		{
			return ServiceAdvertisement.getAdvertisementType();
		}

		@Override
		public Advertisement newInstance()
		{
			return new ServiceAdvertisement();
		}

		@Override
		public Advertisement newInstance(Element root)
		{
			ServiceAdvertisement adv = new ServiceAdvertisement();
			Element element = (Element) root.getChildren(ServiceNameTag)
					.nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setServiceName((String) element.getValue());
			}
			element = (Element) root.getChildren(PipeAdvIDTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setPipeAdvID((String) element.getValue());
			}
			element = (Element) root.getChildren(SolverAdvIDTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setSolverAdvID((String) element.getValue());
			}
			element = (Element) root.getChildren(SolverIDTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setSolverID((String) element.getValue());
			}
			element = (Element) root.getChildren(IDTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setId((String) element.getValue());
			}
			
			return adv;
		}

	}

	public Document getDocument(MimeMediaType mimeMediaType)
	{
		StructuredDocument doc = StructuredDocumentFactory
				.newStructuredDocument(mimeMediaType, getAdvertisementType());
		Element element = doc.createElement(ServiceNameTag, ServiceName);
		doc.appendChild(element);

		element = doc.createElement(PipeAdvIDTag, pipeAdvID);
		doc.appendChild(element);
		
		element = doc.createElement(SolverAdvIDTag, solverAdvID);
		doc.appendChild(element);
		
		element = doc.createElement(SolverIDTag, solverID);
		doc.appendChild(element);
		
		element = doc.createElement(IDTag, id);
		doc.appendChild(element);
		
		return doc;
	}

	public String getServiceName()
	{
		return ServiceName;
	}

	public void setServiceName(String serviceName)
	{
		ServiceName = serviceName;
	}

	public String getPipeAdvID()
	{
		return pipeAdvID;
	}

	public void setPipeAdvID(String pipeAdvID)
	{
		this.pipeAdvID = pipeAdvID;
	}

	public String getSolverAdvID()
	{
		return solverAdvID;
	}

	public void setSolverAdvID(String solverAdvID)
	{
		this.solverAdvID = solverAdvID;
	}

	public String getSolverID()
	{
		return solverID;
	}

	public void setSolverID(String solverID)
	{
		this.solverID = solverID;
	}

}
